import 'dart:async';

import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';

/// 带按钮的输入框
class BaseRequestFormInputWithButton extends StatefulWidget {
  BaseRequestFormInputWithButton({Key key, @required this.request}) : super(key: key);

  final BaseHttpRequestModel request;

  @override
  _BaseRequestFormInputWithButtonState createState() => _BaseRequestFormInputWithButtonState();
}

class _BaseRequestFormInputWithButtonState extends State<BaseRequestFormInputWithButton> {
  // 按钮状态  是否可点击
  bool _isButtonEnable = true;

  // 初始文本
  String _buttonText = '发送验证码';

  //初始倒计时时间
  int _count = 60;

  //倒计时的计时器
  Timer _timer;

  BaseRequestFormProvider _baseRequestFormProvider;

  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);
    return BaseRequestFormContainer(
      request: widget.request,
      child: Row(
        children: [
          Expanded(child: _buildTextFieldWidget()),
          SizedBox(width: 8.0),
          Container(
            child: TextButton(
              child: Text(_buttonText),
              onPressed: _isButtonEnable == true ? _sendCode : null,
              style: TextButton.styleFrom(padding: EdgeInsets.zero),
            ),
            width: 90.0,
          ),
        ],
      ),
    );
  }

  Widget _buildTextFieldWidget() {
    return BaseTextField(
      value: _baseRequestFormProvider.getDataSource(widget.request) ?? '',
      readOnly: _baseRequestFormProvider.isReadonly(widget.request),
      hintText: widget.request.notes,
      onChanged: (String value) {
        _baseRequestFormProvider.setDataSource(widget.request, value);
      },
    );
  }

  void _sendCode() {
    String text = _baseRequestFormProvider.getDataSource(widget.request);
    if (BaseUtils.isNotEmpty(text)) {
      if (this._isButtonEnable == true) {
        _send(text);
        return;
      }
    }
    BaseUtils.showToast('请先输入${widget.request.name}');
  }

  void _initTimer() {
    _timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
      _count--;
      setState(() {
        if (_count == 0) {
          timer.cancel(); // 倒计时结束取消定时器
          _isButtonEnable = true; // 按钮可点击
          _count = 60; // 重置时间
          _buttonText = '发送验证码'; // 重置按钮文本
        } else {
          _buttonText = '重新发送($_count)'; //更新文本内容
        }
      });
    });
  }

  void _send(String text) async {
    String inputApi = widget.request.inputApi;
    BaseComponentUtils.showLoadingDialog(context, hint: '正在发送验证码');
    BaseHttpResultModel result = await dioUtil.fetch(inputApi, method: BaseHttpMethodEnum.GET, params: {widget.request.fieldName: text});
    BaseComponentUtils.hideLoadingDialog(context);
    if (result.success == true) {
      setState(() {
        _isButtonEnable = false;
      });
      _initTimer();
    }
  }

  @override
  void dispose() {
    _timer?.cancel();
    _timer = null;
    super.dispose();
  }
}
